//
//  UserAccount.swift
//  MicroBlog
//
//  Created by  NorthCity on 15/11/6.
//  Copyright © 2015年  NorthCity. All rights reserved.
//

import UIKit
///用户账号模型
class UserAccount: NSObject, NSCoding {
//    接收令牌 access_token
    var access_token:String?
//    接收令牌的生命周期 expires_in,单位是秒
    var expires_in:NSTimeInterval = 0{
//        计算过期日期
        didSet{
            expirseDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
//  过期日期
    var expirseDate:NSDate?
//    当前授权用户UID
    var uid:String?
// 用户昵称
    var screen_name: String?
// 用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
//MARK: - 键值归档和解档
/// 归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expirseDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
/// 解档
      required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token")as?String
        expirseDate = aDecoder.decodeObjectForKey("expiresDate")as?NSDate
        uid = aDecoder.decodeObjectForKey("uid")as?String
        screen_name = aDecoder.decodeObjectForKey("screen_name")as?String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large")as?String
    }
//MARK: -  重写 description 方法,方便调试
    override var description:String{
        get{
            
            let keys = ["access_token", "expires_in", "expirseDate","uid","screen_name","avatar_large"]
            
            return dictionaryWithValuesForKeys(keys).description
        }
        
    }
}

